package com.cinc.mdm.modules.processRoute.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cinc.mdm.modules.processRoute.entity.MdmProcessRouteClass;
import com.cinc.mdm.modules.processRoute.entity.MdmProcessRouteDetail;
import com.cinc.mdm.modules.processRoute.mapper.MdmProcessRouteClassMapper;
import com.cinc.mdm.modules.processRoute.mapper.MdmProcessRouteDetailMapper;
import com.cinc.mdm.modules.processRoute.service.IMdmProcessRouteClassService;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @Description: mdm_process_route_class
 * @Author: jeecg-boot
 * @Date:   2024-05-04
 * @Version: V1.0
 */
@Service
public class MdmProcessRouteClassServiceImpl extends ServiceImpl<MdmProcessRouteClassMapper, MdmProcessRouteClass> implements IMdmProcessRouteClassService {

    @Autowired
    private MdmProcessRouteDetailMapper mdmProcessRouteDetailMapper;

    @Transactional
    @Override
    public void saveMain(MdmProcessRouteClass mdmProcessRouteClass, List<MdmProcessRouteDetail> processRouteDetailList) {
        baseMapper.insert(mdmProcessRouteClass);
        if (processRouteDetailList != null && processRouteDetailList.size() > 0) {
            for (MdmProcessRouteDetail entity : processRouteDetailList) {
                //外键设置
                entity.setClassId(mdmProcessRouteClass.getId());
                entity.setMainId(mdmProcessRouteClass.getMainId());
                //工序控制码设置
                Map<String, String> controlCodeAndName = mdmProcessRouteDetailMapper.selectProcessControlCodeAndName(entity.getControlId());
                entity.setControlCode(controlCodeAndName.get("code"));
                entity.setControlDesc(controlCodeAndName.get("name"));
                mdmProcessRouteDetailMapper.insert(entity);
            }
        }
    }

    @Transactional
    @Override
    public void updateMain(MdmProcessRouteClass mdmProcessRouteClass, List<MdmProcessRouteDetail> processRouteDetailList) {
        baseMapper.updateById(mdmProcessRouteClass);

        //1.先删除子表数据
        mdmProcessRouteDetailMapper.deleteByClassId(mdmProcessRouteClass.getId());

        //2.子表数据重新插入
        if (processRouteDetailList != null && processRouteDetailList.size() > 0) {
            for (MdmProcessRouteDetail entity : processRouteDetailList) {
                //外键设置
                entity.setClassId(mdmProcessRouteClass.getId());
                entity.setMainId(mdmProcessRouteClass.getMainId());
                //工序控制码设置
                Map<String, String> controlCodeAndName = mdmProcessRouteDetailMapper.selectProcessControlCodeAndName(entity.getControlId());
                entity.setControlCode(controlCodeAndName.get("code"));
                entity.setControlDesc(controlCodeAndName.get("name"));

                mdmProcessRouteDetailMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional
    public void delMain(String id) {
        mdmProcessRouteDetailMapper.deleteByClassId(id);
        baseMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for (Serializable id : idList) {
            mdmProcessRouteDetailMapper.deleteByClassId(id.toString());
            baseMapper.deleteById(id);
        }
    }

    @Override
    public String generateClassNo(String mainId, String pid) {
        if(oConvertUtils.isEmpty(mainId) && oConvertUtils.isEmpty(pid)){
            throw new RuntimeException("参数错误");
        }
        String classNo = null;
        LambdaQueryWrapper<MdmProcessRouteClass> query = new LambdaQueryWrapper<>();
        if(oConvertUtils.isNotEmpty(pid)){
            String maxClassNo = baseMapper.selectMaxClassNoByPid(pid);
            if(oConvertUtils.isEmpty(maxClassNo)){
                MdmProcessRouteClass mdmProcessRouteClass = baseMapper.selectById(pid);
                classNo = mdmProcessRouteClass.getClassNo() + ".1";
            }else{
                int num = Integer.parseInt(maxClassNo.substring(maxClassNo.lastIndexOf(".")+1)) + 1;
                classNo = maxClassNo.substring(0, maxClassNo.lastIndexOf(".")) + "." + num;
            }
        }else{
            query.eq(MdmProcessRouteClass::getMainId, mainId);
            query.isNull(MdmProcessRouteClass::getPid);
            int topCount = baseMapper.selectCount(query);
            if(topCount == 0){
                classNo = "1";
            }else{
                classNo = topCount + 1 + "";
            }
        }

        return classNo;
    }

}
